package cn.zxf.note.security;

import cn.zxf.note.common.exception.NoAuthException;
import cn.zxf.note.user.user.UserRepository;
import cn.zxf.note.user.user.UserStatusEnum;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.Objects;

@Component
public class UserAuthService {

    @Autowired
    private UserRepository repos;

    public void validate(Integer userId) {
        if (userId == null)
            throw NoAuthException.ofByNull();
        Integer status = repos.findStatus(userId);
        if (!Objects.equals(status, UserStatusEnum.NORMAL.code))
            throw NoAuthException.ofByInvalid(userId);
    }

    public void validate(Integer curUserId, Integer expectUserId) {
        this.validate(curUserId);
        if (expectUserId == null)
            return;
        if (!Objects.equals(curUserId, expectUserId))
            throw NoAuthException.ofByHacker(curUserId);
    }

}
